# -*- coding: utf-8 -*- 
# 作者:飞虎
# 开发时间:2022/6/1 23:00
import random

from PyQt5.Qt import *
from ui_QTableWidget_Test import Ui_Form


class Window(QWidget, Ui_Form):
	def __init__(self):
		super().__init__()
		self.setWindowTitle('QTableWidget学习')
		self.resize(500, 500)
		self.setup_ui()

	def setup_ui(self):
		self.setupUi(self)
		self.layout = QHBoxLayout()
		self.layout.addWidget(self.tableWidget)
		self.setLayout(self.layout)

		# 增加一个按钮组
		self.group = QButtonGroup()
		self.group.setExclusive(True)

		# 设置标题
		label_list = ["", '车次', '始发地', '目的地', '座位']
		self.tableWidget.setHorizontalHeaderLabels(label_list)

		# 表格头随控件变化(水平方向占满窗体)
		self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
		self.tableWidget.verticalHeader().setVisible(False)

		self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
		# 设置数据按车次数据排序
		self.tableWidget.sortItems(1, Qt.AscendingOrder)

		car_list = ['K211', 'D342', 'Z21', 'G1432', 'T211', 'D9540', 'K234']
		area_list = ['北京', '上海', '深圳', '广州', '新疆', '武汉', '哈尔滨']
		seat_info = {
			"K": ['硬座', '硬卧', '软卧', '无座'],
			'T': ['硬座', '硬卧', '软卧', '无座'],
			'Z': ['硬座', '硬卧', '软卧', '无座'],
			'D': ['二等座', '一等座', '商务座', '无座'],
			'G': ['二等座', '一等座', '商务座']
		}
		# 开始向表格中填充数据
		for row in range(len(car_list)):
			for column in range(len(label_list)):
				# 列为0,填入普通文字
				if column == 0:
					self.radio = QRadioButton()
					self.group.addButton(self.radio)
					self.radio.clicked.connect(self.radio_check)

					v = QVBoxLayout()
					v.addWidget(self.radio)
					v.setAlignment(Qt.AlignCenter)
					w = QWidget()
					w.setLayout(v)

					self.tableWidget.setCellWidget(row, column, w)
				elif column == 1:
					item = QTableWidgetItem(car_list[row])
					# item = QTableWidgetItem(area_list[random.randint(0, len(area_list) - 1)])
					# item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
					self.tableWidget.setItem(row, column, item)
				elif column == 2 or column == 3:
					item = QTableWidgetItem(area_list[random.randint(0, len(area_list) - 1)])
					self.tableWidget.setItem(row, column, item)

				else:
					self.comb = QComboBox()
					self.comb.setEnabled(False)
					self.comb.setStyleSheet('background-color:rgb(198,199,206)')
					self.comb.addItems(seat_info[car_list[row][0]])
					self.comb.currentIndexChanged.connect(self.comb_change)
					self.tableWidget.setCellWidget(row, column, self.comb)

		self.tableWidget.clicked.connect(self.click_position)

	def comb_change(self):
		row = self.tableWidget.currentRow()
		column = self.tableWidget.currentColumn()
		print('row:{},column:{},seat:{}'.format(row, column, self.sender().currentText()))

	def click_position(self):
		row = self.tableWidget.currentRow()
		column = self.tableWidget.currentColumn()
		print('当前位置:(%s,%s)' % (row, column))

	def radio_check(self):
		for index, button in enumerate(self.group.buttons()):
			if button.isChecked():
				self.tableWidget.cellWidget(index, 4).setEnabled(True)
				self.tableWidget.cellWidget(index, 4).setStyleSheet('background-color:rgb(255,255,255)')
				print('当前默认座位:%s' % (self.tableWidget.cellWidget(index, 4).currentText()))
			else:
				self.tableWidget.cellWidget(index, 4).setEnabled(False)
				self.tableWidget.cellWidget(index, 4).setStyleSheet('background-color:rgb(198,199,206)')


if __name__ == '__main__':
	import sys

	app = QApplication(sys.argv)
	window = Window()
	window.show()
	sys.exit(app.exec_())
